<template>
  <el-table :data="list" border :span-method="colSpanMethod">
    <el-table-column fixed width="410" label="商品信息">
      <template #default="scope">
        <div class="flex items-center">
          <img class="w-20 h-20" :src="scope.row.url" />
          <div style="margin-left: 10px">
            <el-tooltip
              class="box-item"
              effect="dark"
              :content="scope.row.name"
              placement="top"
            >
              <div
                style="width: 180px"
                class="whitespace-nowrap overflow-hidden overflow-ellipsis"
              >
                {{ scope.row.name }}
              </div>
            </el-tooltip>

            <div>
              <span style="color: #999">{{ scope.row.codeTitle }}</span
              >{{ scope.row.code }}
            </div>
          </div>
          <div class="ml-3" @click="handleAdd(scope.$index, scope.row)">
            <el-button type="primary" size="small">
              添加SKU
            </el-button>
          </div>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="SKU信息" width="130">
      <template #default="scope">
        <div v-if="scope.row.add">
          <div style="color: #409eff">
            {{ scope.row.color }} / {{ scope.row.size }}
          </div>
          <div class="font-bold">
            <span class="font-normal" style="color: #999; margin-right: 10px"
              >市场价:</span
            >¥{{ scope.row.marketPrice }}
          </div>
          <div class="font-bold">
            <span class="font-normal" style="color: #999; margin-right: 10px"
              >库存价:</span
            >¥{{ scope.row.inventoryPrice }}
          </div>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="入库数量" width="130">
      <template #default="scope">
        <el-input-number
          size="small"
          v-if="scope.row.add"
          style="width: 90px"
          v-model="scope.row.inventoryNum"
          @blur="
            (e) => {
              const value = e.target.value;
              handleBlurPrice(value, scope.$index, scope.row);
            }
          "
          :controls="false"
        />
      </template>
    </el-table-column>
    <el-table-column label="推荐库位" width="400">
      <template #default="scope">
        <div class="flex items-end" v-if="scope.row.add">
          <div class="flex flex-col">
            <div
              style="margin-top: 10px"
              class="flex items-center recommend-item"
              v-for="(item, index) in scope.row.recommend"
              :key="index"
            >
              <el-cascader
                :ref="`recommend-casvader-${scope.$index}-${index}`"
                :options="options"
                size="small"
                separator="-"
                v-model="scope.row.recommend[index].location"
                placeholder="请选择库位"
                @visible-change="handleChangeCasvader(scope.$index, scope.row, index, `recommend-casvader-${scope.$index}-${index}`)"
              >
                <template #default="{ node, data }">
                  <div class="flex items-center justify-between">
                    <span>{{ data.label }}</span>
                    <span
                      style="
                        height: 24px;
                        line-height: 24px;
                        padding-left: 3px;
                        padding-right: 3px;
                        border-radius: 4px;
                        font-size: 12px;
                      "
                      :class="[data.isSpell ? 'normal' : 'spell']"
                      v-if="node.isLeaf"
                    >
                      <span v-if="data.isSpell">拼/</span>可用{{ data.num }}
                    </span>
                  </div>
                </template>
              </el-cascader>
              <div>
                <el-input-number
                  size="small"
                  style="width: 60px; margin-left: 10px"
                  v-model="scope.row.recommend[index].recommendNum"
                  :controls="false"
                  @blur="
                    (e) => {
                      const value = e.target.value;
                      handleBlur(value, scope.$index, scope.row, index);
                    }
                  "
                />
              </div>
              <div
                style="margin-left: 10px; height: 32px"
                v-if="!(scope.row.recommend.length === 1 && index === 0)"
              >
                <el-button
                  icon="el-icon-minus"
                  size="small"
                  @click="handleDelNum(scope.$index, scope.row, index)"
                  style="height: 100%; background: rgb(241 241 241)"
                />
              </div>
            </div>
          </div>
          <div style="height: 32px; margin-left: 10px">
            <el-button
              icon="el-icon-plus"
              size="small"
              @click="handleAddNum(scope.$index, scope.row)"
              style="height: 100%; background: rgb(241 241 241)"
            />
          </div>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="入库单价" width="200">
      <template #default="scope">
        <div v-if="scope.row.add">
          <el-input-number
            size="small"
            style="width: 90px"
            v-model="scope.row.unitPrice"
            :controls="false"
            @blur="
              (e) => {
                const value = e.target.value;
                handleBlurPrice(value, scope.$index, scope.row);
              }
            "
          />
          <div
            style="font-size: 12px; color: red"
            v-if="scope.row.unitPrice && scope.row.inventoryNum"
          >
            <div>加权平均价: {{ scope.row.averagePrice }}</div>
            <div>库存数量{{ scope.row.storeNum }}</div>
          </div>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="批次" width="150">
      <template #default="scope">
        <div v-if="scope.row.add">{{ scope.row.batch }}</div>
      </template>
    </el-table-column>
    <el-table-column label="备注信息" width="130">
      <template #default="scope">
        <el-input
          size="small"
          v-if="scope.row.add"
          v-model="scope.row.remark"
          placeholder="输入备注"
        />
      </template>
    </el-table-column>
    <el-table-column label="操作" width="120">
      <template #default="scope">
        <el-button
          v-if="scope.row.add"
          plain
          type="primary"
          size="small"
          @click="handleRemove(scope.$index, scope.row)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import { options as optionList } from "../constants";

export default {
  name: "inventoryTable",
  props: {
    list: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      options: optionList(), // 正确初始化 options
      tableData: [],
    };
  },
  watch: {
    list: {
      handler(val) {
        this.tableData = val;
      },
      deep: true,
      immediate: true,
    },
  },
  emits: [
    "onAdd",
    "onAddNum",
    "onDelNum",
    "onRemove",
    "onUpdateNum",
    "onBlurPrice",
  ],

  methods: {
    colSpanMethod(val) {
      const { rowIndex, columnIndex } = val
      if (columnIndex === 0) {
        const _row = this.getSpanArr(rowIndex, "code");
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col,
        };
      }
    },
    getSpanArr(index, key) {
      const spanArr = [];
      let pos;
      this.tableData.forEach((_, i) => {
        if (i === 0) {
          spanArr.push(1);
          pos = 0;
        } else {
          if (this.tableData[i][key] === this.tableData[i - 1][key]) {
            spanArr[pos] += 1;
            spanArr.push(0);
          } else {
            spanArr.push(1);
            pos = i;
          }
        }
      });
      return spanArr[index];
    },
    handleAdd(index, row) {
      this.$emit("onAdd", index, row);
    },
    handleAddNum(index, row) {
      this.$emit("onAddNum", index, row);
    },
    handleDelNum(index, row, colIndex) {
      const { recommend } = row;
      if (recommend.length === 1) {
        this.$message.warning("至少需要一个库位");
        return;
      }
      this.$emit("onDelNum", index, row, colIndex);
    },
    handleRemove(index, row) {
      this.$emit("onRemove", index, row);
    },
    handleBlur(value, index, row, index2) {
      const all = row.recommend.reduce((acc, cur) => {
        return acc + Number(cur?.recommendNum || 0);
      }, 0);
      if (all > (row?.inventoryNum || 0)) {
        this.$message.warning("请检查入库数量, 请确认后操作");
        this.$emit("onUpdateNum", value, index, row, index2);
        return;
      }
    },
    handleBlurPrice(value, index, row) {
      this.$emit("onBlurPrice", value, index, row);
    },
    handleChangeCasvader(index, row, recommendIndex, ref) {
      console.log(this.$refs[ref][recommendIndex].getCheckedNodes()[0], '2222', ref)
      
    }
  },
};
</script>

<style scoped>
.normal {
  color: #ff7600;
  background: rgba(255, 118, 0, 0.1);
}

.spell {
  color: #409eff;
  background: rgba(64, 158, 255, 0.1);
}

.recommend-item:first-of-type {
  margin-top: 0 !important;
}
.recommend-item {
  position: relative;
}
.recommend-item__fixed {
  position: absolute;
  right: 100px;
  z-index: 10;
  font-size: 12px;
}
</style>
